/*
	@ author: leeenx
	@ 消除星星
*/

// 向前兼容
// import 'babel-polyfill'; 

import Model from './core/Model.es6';
import View from './core/View.es6';
import Control from './core/Control.es6';
import qttGame from './core/jssdk-1.0.0';

// let hub = new EventHub({
//   targetOrigin: 'http://lo', // the origin you want to send to
//   originRegex: /^(https?):\/\/.*(my-domain)(\.com)$/,
//   targetWindow: window.parent, // the window to postMessage to
//   hubId: -1,
//   verbose: true,
// });
const formUrl='http://139.196.218.92:4212';
const toUrl='http://139.196.218.92:4213';

class Popstar {
	constructor(config) {

		this.getUser();
		// mvc 初始化
		this.model = new Model();
		this.view = new View(config.view);
		this.userToken;
		this.order;
		// mv 由于 c 控制
		this.constrol = new Control(
			this.model,
			this.view,
			config.total
		);
		// 挂载 event
		this.event = this.constrol.event;
		// 总得分
		Reflect.defineProperty(
			this,
			"total", {
				get: () => this.constrol.total,
				set: value => this.constrol.total = value
			}
		);
		// 当前关卡 ---- 只读
		Reflect.defineProperty(
			this,
			"level", {
				get: () => this.constrol.curLevel
			}
		);
	}
	getUser() {
		qttGame.genSignUser('test002').then((e) => {
			console.log(e)
		});
	}
	/**
	 * @act:花费金额
	 *  did:道具ID
	 */
	tobuy(data) {
		console.log(data)
		qttGame.genOrder(data).then((e) => {
			this.order = e.data;
			console.log(e, 'tobuy')
		})
		container.innerHTML = '';
		var iframe = document.createElement('iframe');
		iframe.id = "frame";
		iframe.style = "height:30rem;";
		iframe.name = "polling";
		iframe.src = toUrl + "/recharge";
		container.appendChild(iframe);
		iframe.onload = function () {
			window.onmessage = function (e) {
				switch (e.data.type) {
					case 'payOk':
						popstar.resume();
						popstar.updateGj();
						popstarAd.style.display = "none";
						container.innerHTML='';
						break;
				}
			}
		}

	}
	// 开始游戏
	enter(level) {
		this.constrol.enter(level);
	}
	// 下一关
	next() {
		this.constrol.next();
	}
	// 暂停
	pause() {
		this.constrol.pause();
	}
	// 恢复
	resume() {
		this.constrol.resume();
	}
	updateGj(){
		this.constrol.updateGj();
	}
	// 销毁
	destroy() {
		this.constrol.destroy();
	}
}

window.Popstar = Popstar;